package cn.fyl.user.controller;

import cn.fyl.basic.util.JsonResult;
import cn.fyl.basic.util.PageList;

import cn.fyl.user.domain.LoginInfo;
import cn.fyl.user.query.LoginInfoQuery;
import cn.fyl.user.service.ILoginInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/loginInfo")
public class LoginInfoController {

    @Autowired
    private ILoginInfoService LoginInfoService;

    /**
     * 查询所有
     * @return
     */
    @GetMapping
    public List<LoginInfo> findAll(){
        return LoginInfoService.loadAll();
    }

    /**
     * 查询一个
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public LoginInfo findById(@PathVariable("id") Long id){
        return LoginInfoService.loadById(id);
    }

    /**
     * 删除操作
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long id){
        try {
            LoginInfoService.remove(id);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }

    /**
     * 添加和修改操作
     * @param LoginInfo
     * @return
     */
    @PostMapping
    public JsonResult save(@RequestBody LoginInfo LoginInfo){
        try {
            if(LoginInfo.getId()==null){
                LoginInfoService.save(LoginInfo);
            }else {
                LoginInfoService.update(LoginInfo);
            }
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }

    }

    /**
     * 分页查询
     * @param
     * @return
     */
    @PutMapping
    public PageList<LoginInfo> queryPage(@RequestBody LoginInfoQuery query){
        try {
            return LoginInfoService.queryData(query);
        } catch (Exception e) {
            e.printStackTrace();
            return new PageList<LoginInfo>();
        }
    }

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    public JsonResult patchDelete(@RequestBody List<Long> ids){
        try {
            LoginInfoService.patchDelete(ids);
            return JsonResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return JsonResult.me().setMsg("操作失败");
        }
    }
}
